package com.mathassistant.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.event.ShowEditorEvent;

public class ComponentAnwserPresenter implements Presenter{

	public interface Display{
		HasClickHandlers getAnchorClick();
		HasClickHandlers getImageClick();
		HasHTML getContentHTML();
		
		void setTileLable(String title);
		void setData(String html);
		Widget asWidget();
	}
	
	private int currentIndex;
	private String title;
	private String contentHTML;
	private final Display display;
	private final SimpleEventBus eventBus;
	
	public ComponentAnwserPresenter(int index, String title, String contentHTML,SimpleEventBus eventBus,Display view){
		setCurrentIndex(index);
		this.contentHTML = contentHTML;
		this.title = title;
		this.display = view;
		this.eventBus = eventBus;
		
		initFillData();
		bind();
	}
	
	private void bind() {
		// TODO Auto-generated method stub
		display.getAnchorClick().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new ShowEditorEvent(display.getContentHTML()));
			}
		});
		display.getImageClick().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new ShowEditorEvent(display.getContentHTML()));
			}
		});
	}

	private void initFillData() {
		// TODO Auto-generated method stub
		if(title != null){
			this.display.setTileLable(title);
		}
		if(contentHTML != null){
			this.display.setData(contentHTML);
		}
	}

	
	@Override
	public void go(HasWidgets container) {}

	@Override
	public void go() {}

	
	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	public Display getDisplay() {
		return display;
	}

	public SimpleEventBus getEventBus() {
		return eventBus;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setContentHTML(String contentHTML) {
		this.contentHTML = contentHTML;
	}

	public String getContentHTML() {
		return contentHTML;
	}

}
